package john.wenzi.server;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import john.wenzi.server.utils.JdbcUtil;

public class JdbcTest01 {
	public static void main(String[] args) throws IOException{
        try{
            runTest();
        	
        	
        }catch(SQLException ex){
            for(Throwable t:ex)
                t.printStackTrace();
        } catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
   
    public static void runTest() throws SQLException,IOException, ClassNotFoundException{
        try(Connection conn = getConnection())
        {
            Statement stat = conn.createStatement();
            String sql1 = "CREATE TABLE upload_record(" + 
            		"uuid_name VARCHAR(200)," + 
            		"file_size VARCHAR(100)," + 
            		"file_type VARCHAR(100)," + 
            		"file_name VARCHAR(200)," + 
            		"create_date timestamp," + 
            		"file_path VARCHAR(200)," + 
            		"encrypt_digital_envelope VARCHAR(1000)," + 
            		"status VARCHAR(2))";
            
            String sql2 = "drop table upload_record";
            //stat.executeUpdate(sql1);

            //stat.executeUpdate("INSERT INTO first_table VALUES('gui')");
            //stat.executeUpdate("INSERT INTO first_table VALUES('你好，世界')");

            try(ResultSet result = stat.executeQuery("select * from upload_record order by create_date desc offset 0 rows fetch next 10 rows only")){
                while(result.next()) 
                    System.out.println(result.getString("uuid_name")+"--"+result.getString("file_size")+"--"+result.getString("file_type")+"--"+result.getString("file_name")+"--"+result.getDate("create_date")+"--"+result.getString("file_path")+"--"+result.getString("status")+"--"+result.getString("encrypt_digital_envelope"));
            }
            //stat.executeUpdate("DROP TABLE first_table");

        }
    }
    
    public static Connection getConnection() throws SQLException,IOException, ClassNotFoundException{
        Properties props = new Properties();
        try(InputStream in = JdbcTest01.class.getClassLoader().getResourceAsStream("db.properties")){
            props.load(in);
        }
        String drivers = props.getProperty("jdbc.drivers");
        if(drivers != null)
            System.setProperty("jdbc.drivers",drivers);
        String url = props.getProperty("jdbc.url");
        String username = props.getProperty("jdbc.username");
        String password = props.getProperty("jdbc.password");
        //Class.forName(drivers);
        return DriverManager.getConnection(url,username,password);
    }


}
